﻿using System;
using System.Linq;
using Looptribe.Moodlin8.Model;

namespace Looptribe.Moodlin8.ViewModel
{
    public class AssignmentWrapper : Wrapper<Assignment>
    {
        private readonly Course _course;

        public AssignmentWrapper(Assignment assignment, Course course) : base(assignment)
        {
            _course = course;
        }

        public string Course { get { return _course.Fullname; } }

        public string Title { get { return WrappedObject.Name; } }
        public DateTime DueDateFormatted
        {
            get { return WrappedObject.DueDate.HasValue ? WrappedObject.DueDate.Value : DateTime.MinValue; }
        }

        public string Intro { get { return WrappedObject.Intro; } }

        public string Grade
        {
            get
            {
                if (!WrappedObject.Grades.Items.Any(a => a.Grades.Any()))
                    return null;
                return WrappedObject.Grades.Items.First().Grades.First().StrGrade;
            }
        }

        public string GradeExtended
        {
            get
            {
                if (!WrappedObject.Grades.Items.Any(a => a.Grades.Any()))
                    return null;
                var item = WrappedObject.Grades.Items.First();
                var grade = item.Grades.First();
                if (grade.Grade.HasValue)
                    return ((Math.Abs(grade.Grade.Value) - item.GradeMin) / (item.GradeMax - item.GradeMin)).ToString("P0");
                return item.Grades.First().StrLongGrade;
            }
        }
    }
}
